package com.sinvo.oa.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinvo.oa.entity.model.process.Process;
import com.sinvo.oa.entity.model.process.ProcessTemplate;
import com.sinvo.oa.entity.model.process.ProcessType;
import com.sinvo.oa.entity.vo.process.ApprovalVo;
import com.sinvo.oa.entity.vo.process.ProcessFormVo;
import com.sinvo.oa.entity.vo.process.ProcessVo;
import com.sinvo.oa.exception.CustomException;
import com.sinvo.oa.result.R;
import com.sinvo.oa.result.ResultCode;
import com.sinvo.oa.service.ProcessService;
import com.sinvo.oa.service.ProcessTemplateService;
import com.sinvo.oa.service.ProcessTypeService;
import com.sinvo.oa.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Description 用户端审批控制器
 */
@Api(tags = "用户审批流管理")
@RestController
@CrossOrigin
@RequestMapping(value = "user/process")
public class UserProcessController {

    @Autowired
    private ProcessTypeService processTypeService;

    @Autowired
    private ProcessTemplateService processTemplateService;

    @Autowired
    private ProcessService processService;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 查询所有的审批分类以及其中的审批模板
     * ProcessType processTemplateList属性存放其模板
     *
     * @return R
     */
    @ApiOperation(value = "查询所有审批分类")
    @GetMapping("findProcessType")
    public R findProcessType() {
        List<ProcessType> typeList = processTypeService.findProcessType();
        return R.success(typeList);
    }

    /**
     * 获取审批模板数据
     *
     * @param id templateId
     * @return R
     */
    @ApiOperation(value = "根据id获取模板数据")
    @GetMapping("getProcessTemplate/{id}")
    public R getProcessTemplate(@PathVariable Long id) {
        //验证参数
        if (StringUtils.isEmpty(id)) {
            throw new CustomException(ResultCode.DATA_ERROR.getCode(), "非法参数");
        }
        ProcessTemplate processTemplate = processTemplateService.getById(id);
        return R.success(processTemplate);
    }

    /**
     * 启动流程
     *
     * @param processFormVo
     * @return R
     */
    @ApiOperation(value = "启动流程")
    @PostMapping("startUp")
    public R startUp(@RequestBody ProcessFormVo processFormVo) {
        processService.startUp(processFormVo);
        return R.success();
    }

    /**
     * 查询当前用户待处理的审批
     *
     * @param pageNum  当前页
     * @param pageSize 页大小
     * @return R
     */
    @ApiOperation(value = "查询待处理")
    @GetMapping("findPending/{pageNum}/{pageSize}")
    public R findPending(@PathVariable Long pageNum, @PathVariable Long pageSize) {
        Page<Process> page = new Page<>(pageNum, pageSize);
        Page<ProcessVo> voPage = processService.getPending(page);
        return R.success(voPage);
    }

    /**
     * 查看审批详情
     * @param id processId
     * @return R
     */
    @ApiOperation(value = "查看审批详情")
    @GetMapping("show/{id}")
    public R show(@PathVariable Long id) {
        Map<String, Object> map = processService.showDetail(id);
        return R.success(map);
    }

    /**
     * 进行审批
     * @param approvalVo status
     * @return R
     */
    @ApiOperation(value = "审批操作")
    @PostMapping("approve")
    public R approve(@RequestBody ApprovalVo approvalVo) {
        processService.approveProcess(approvalVo);
        return R.success();
    }

    /**
     * 查询已处理审批
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "查询已处理")
    @GetMapping("findProcessed/{pageNum}/{pageSize}")
    public R findProcessed(@PathVariable Long pageNum, @PathVariable Long pageSize) {
        Page<Process> page = new Page<>(pageNum, pageSize);
        Page<ProcessVo> voPage = processService.getProcessed(page);
        return R.success(voPage);
    }

    /**
     * 查询已发起审批
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "查询已发起")
    @GetMapping("findStarted/{pageNum}/{pageSize}")
    public R findStarted(@PathVariable Long pageNum, @PathVariable Long pageSize) {
        Page<ProcessVo> page = new Page<>(pageNum, pageSize);
        Page<ProcessVo> voPage = processService.findStarted(page);
        return R.success(voPage);
    }

    /**
     * 获取用户信息
     * @return
     */
    @ApiOperation(value = "获取登录用户信息")
    @GetMapping("getCurrentUser")
    public R getCurrentUser() {
        Map<String, Object> map = sysUserService.getCurrentUser();
        return R.success(map);
    }
}
